<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/9/3
 * Time: 12:56
 */

namespace app\index\logic;



use think\Db;
use Think\Exception;
use think\Log;

class PayLogic
{
    /**
     * @param $order_number string 订单编号
     * @param $money float 总金额
     * @param  $type 0微信支付 1支付宝支付 2银行卡支付
     * @return int 0余额不足 1支付成功 2支付失败
     */
    public function weChatPaySuccess($order_number,$money,$type)
    {
        $where['order_number']=$order_number;
        $order = model('Order')->where($where)->find();
        if($order){
//            if($recharge_order['state'] == 0 && is_numeric($money) && $recharge_order['money'] <= $money) {
            if($order['status'] == 0 && is_numeric($money)) {
                return $this->paySuccess($order,$type);//充值成功
            }
        }
        return 2;
    }

    private function paySuccess($order,$type){
        Db::startTrans();
        try{
            $update_order['type'] = $type;
            $update_order['status'] = 1;
            Db::name('Order')
                ->where(array('id'=>$order['id']))
                ->update($update_order);
            Db::name('UserNote')
                ->where(array('id'=>$order['user_note_id']))
                ->update(array('is_solve'=>1));//已经解签
            Db::commit();
            return 1;
        }catch (Exception $e){
            Log::write('paySuccess:'.$e->getMessage());
            Db::rollback();
            return 2;
        }
    }

}